<?php
/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/6/13
 * Time: 23:42
 */

namespace app\common\util;

class CUrl
{
    public static function httpPost($url, $params = '')
    {
        if (!is_string($params)) {
            $params = \http_build_query($params);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        $response = curl_exec($ch);
        $errno = curl_errno($ch);
        if ($errno > 0) {
            trace('http get error:' . $url . '.no:' . $errno, 'error');
        }
        curl_close($ch);
        return $response;
    }

    public static function asyncHttpGet($url)
    {
        $urlinfo = parse_url($url);
        $host = $urlinfo['host'];
        $path = $urlinfo['path'];
        $timeout = 5;
        $port = empty($urlinfo['port']) ? 80 : intval($urlinfo['port']);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
        if (!$fp) {
            ccnn_syslog('asyncHttpGet error:' . $errno . ',url:' . $url);
            return;
        }
        $out = "GET " . $path . " HTTP/1.1\r\n";
        $out .= "host:" . $host . "\r\n";
//        $out .= "content-length:" . strlen($query) . "\r\n";
//        $out .= "content-type:application/x-www-form-urlencoded\r\n";
        $out .= "connection:close\r\n\r\n";
        fputs($fp, $out);
        fclose($fp);
    }

    public static function asyncHttpPost($url, $query = [])
    {
        if (!is_string($query)) {
            $query = \http_build_query($query);
        }
        $urlinfo = parse_url($url);
        $host = $urlinfo['host'];
        $path = $urlinfo['path'];
        $timeout = 5;
        $port = empty($urlinfo['port']) ? 80 : intval($urlinfo['port']);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
        if (!$fp) {
            ccnn_syslog('asyncHttpGet error:' . $errno . ',url:' . $url);
            return;
        }
        $out = "POST " . $path . " HTTP/1.1\r\n";
        $out .= "host:" . $host . "\r\n";
        $out .= "content-length:" . strlen($query) . "\r\n";
        $out .= "content-type:application/x-www-form-urlencoded\r\n";
        $out .= "connection:close\r\n\r\n";
        $out .= $query;
        fputs($fp, $out);
        fclose($fp);
    }

    public static function httpGet($url, $params = '', $headers = [])
    {
        if (!empty($query)) {
            $url .= (strpos($url, '?') === false) ? '?' : '&';
            $url .= is_array($params) ? http_build_query($params) : $params;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSLVERSION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        $errno = curl_errno($ch);
        if ($errno > 0) {
            trace('http get error:' . $url . '.no:' . $errno, 'error');
        }
        curl_close($ch);
        return $response;
    }

    public static function addFile($filename)
    {
        return class_exists('\CURLFile') ? new \CURLFile($filename) : '@' . $filename;
    }

}